home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / Book Demos in Pascal / SpriteEngine / SE Timer / SpriteHandlers.p < prev    next >
Encoding:
Text File  |  1995-04-06  |  4.7 KB  |  160 lines  |  [TEXT/MWPS]

  1. unit SpriteHandlers;
  2.  
  3. interface
  4.  
  5.     uses
  6. {$IFC UNDEFINED THINK_PASCAL}
  7.         Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
  8.         OSUtils, ToolUtils, OSEvents, 
  9. {$ENDC}
  10.         QDOffScreen, SpriteStructure, SpriteTools;
  11.  
  12.     procedure MoveSprite (theSprite: SpritePtr);
  13.     procedure HitSprite (theSprite: SpritePtr; anotherSprite: SpritePtr);
  14.     procedure InitSprites;
  15.  
  16. implementation
  17.  
  18. { Custom handlers - application dependent ***}
  19. {}
  20. {Edit this as necessary. It should always include the following three routines:}
  21. {}
  22. {MoveSprite: move the sprite}
  23. {}
  24. {HitSprite: handle collisions between two sprites}
  25. {}
  26. {InitSprites: Load all faces and create initial sprites }
  27.  
  28.  
  29. var
  30. firstFace, secondFace, thirdFace:GrafPtr;
  31.  
  32.  
  33. Procedure MoveSprite(theSprite:SpritePtr);
  34. var thisTime:LongInt;
  35.  begin 
  36.     thisTime := TickCount;
  37.  
  38.     While theSprite^.lastTime < thisTime Do
  39.      begin 
  40.         theSprite^.lastTime := theSprite^.lastTime + 1;
  41.         theSprite^.speed.v := theSprite^.speed.v + 1; { Simple gravity}
  42.         theSprite^.fixedPointPosition.h := theSprite^.fixedPointPosition.h + theSprite^.speed.h;
  43.         theSprite^.fixedPointPosition.v := theSprite^.fixedPointPosition.v + theSprite^.speed.v;
  44.         theSprite^.position.h := BSR(theSprite^.fixedPointPosition.h , 4);
  45.         theSprite^.position.v := BSR(theSprite^.fixedPointPosition.v , 4);
  46.      End;
  47.  
  48.     if KeepOnScreenFixed(theSprite) then;
  49.  End; (*MoveSprite*)
  50.  
  51.  
  52. Procedure HitSprite(theSprite:SpritePtr; anotherSprite:SpritePtr);
  53. var tempSpeed:Integer;
  54. begin
  55.     If RectSeparate(theSprite, anotherSprite) >= 2 Then { 2 or 3: horizontal, otherwise vertical}
  56.      begin 
  57.         tempSpeed := theSprite^.speed.h;
  58.         theSprite^.speed.h := anotherSprite^.speed.h;
  59.         anotherSprite^.speed.h := tempSpeed;
  60.         theSprite^.fixedPointPosition.h := BSL(theSprite^.position.h , 4);
  61.      End
  62.     Else     
  63.      begin 
  64.         tempSpeed := theSprite^.speed.v;
  65.         theSprite^.speed.v := anotherSprite^.speed.v;
  66.         anotherSprite^.speed.v := tempSpeed;
  67.         theSprite^.fixedPointPosition.v := BSL(theSprite^.position.v , 4);
  68.      End;
  69.  
  70.  End; (*HitSprite*)
  71.  
  72.  
  73. Procedure InitSprites;
  74.     var theSprite:SpritePtr;
  75. (*Load all pictures*)
  76.      begin 
  77.     firstFace := LoadFaceFromCicn(128);            (*cicn resource #128.*)
  78.     secondFace := LoadFaceFromCicn(129);            (*cicn resource #129.*)
  79.     thirdFace := LoadFaceFromCicn(130);            (*cicn resource #130.*)
  80.  
  81. (*Create sprites*)
  82.     theSprite := NewSprite;
  83.     theSprite^.face := firstFace;
  84.     SetPt(theSprite^.fixedPointPosition, BSL(100 , 4), BSL(100 , 4));
  85.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  86.     theSprite^.lastTime := TickCount;
  87.  
  88.     theSprite := NewSprite;
  89.     theSprite^.face := secondFace;
  90.     SetPt(theSprite^.fixedPointPosition, BSL(50 , 4), BSL(50 , 4));
  91.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  92.     theSprite^.lastTime := TickCount;
  93.  
  94.     theSprite := NewSprite;
  95.     theSprite^.face := thirdFace;
  96.     SetPt(theSprite^.fixedPointPosition, BSL(150 , 4), BSL(150 , 4));
  97.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  98.     theSprite^.lastTime := TickCount;
  99.  
  100.  
  101. { extra}
  102.  
  103.     theSprite := NewSprite;
  104.     theSprite^.face := firstFace;
  105.     SetPt(theSprite^.fixedPointPosition, BSL(100 , 4), BSL(100 , 4));
  106.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  107.     theSprite^.lastTime := TickCount;
  108.  
  109.     theSprite := NewSprite;
  110.     theSprite^.face := secondFace;
  111.     SetPt(theSprite^.fixedPointPosition, BSL(50 , 4), BSL(50 , 4));
  112.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  113.     theSprite^.lastTime := TickCount;
  114.  
  115.     theSprite := NewSprite;
  116.     theSprite^.face := thirdFace;
  117.     SetPt(theSprite^.fixedPointPosition, BSL(150 , 4), BSL(150 , 4));
  118.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  119.     theSprite^.lastTime := TickCount;
  120.  
  121.     theSprite := NewSprite;
  122.     theSprite^.face := firstFace;
  123.     SetPt(theSprite^.fixedPointPosition, BSL(100 , 4), BSL(100 , 4));
  124.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  125.     theSprite^.lastTime := TickCount;
  126.  
  127.     theSprite := NewSprite;
  128.     theSprite^.face := secondFace;
  129.     SetPt(theSprite^.fixedPointPosition, BSL(50 , 4), BSL(50 , 4));
  130.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  131.     theSprite^.lastTime := TickCount;
  132.  
  133.     theSprite := NewSprite;
  134.     theSprite^.face := thirdFace;
  135.     SetPt(theSprite^.fixedPointPosition, BSL(150 , 4), BSL(150 , 4));
  136.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  137.     theSprite^.lastTime := TickCount;
  138.  
  139.     theSprite := NewSprite;
  140.     theSprite^.face := firstFace;
  141.     SetPt(theSprite^.fixedPointPosition, BSL(100 , 4), BSL(100 , 4));
  142.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  143.     theSprite^.lastTime := TickCount;
  144.  
  145.     theSprite := NewSprite;
  146.     theSprite^.face := secondFace;
  147.     SetPt(theSprite^.fixedPointPosition, BSL(50 , 4), BSL(50 , 4));
  148.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  149.     theSprite^.lastTime := TickCount;
  150.  
  151.     theSprite := NewSprite;
  152.     theSprite^.face := thirdFace;
  153.     SetPt(theSprite^.fixedPointPosition, BSL(150 , 4), BSL(150 , 4));
  154.     SetPt(theSprite^.speed, Rand(7)-3, Rand(7)-3);
  155.     theSprite^.lastTime := TickCount;
  156.  
  157.  End; (*InitSprites*)
  158. end.
  159.  
  160.